// pages/myvip/myvip.js
import {
  getMyVip,
  stopGo,
  getUserLogin
} from '../../apis/api'
// const dayjs = require("./dayjs")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipCardImg: [
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard01.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard02.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard03.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard04.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard05.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard06.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard07.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard08.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard09.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard10.png',
      'http://xcx.jinchenghealth.com:8003/bgp/vipcard11.png'
    ],
    vipList: [],
    text: '',
    token: '',
    thisShow: false,
    isLog: 0,
    logTip: true,
  },
  getLog: function () {
    if (!!!wx.getStorageSync('token')) wx.setStorageSync('token', 'token null')
    getUserLogin().then(async res => {
      if (res.code === 200) {
        this.setData({
          isLog: res.data
        })
        if (!!this.data.token && !!this.data.isLog) {
          await this.getAllVipCards()
          if (this.data.logTip) {
            // setTimeout(() => {
            //   wx.showToast({
            //     title: '登录成功',
            //     duration: 1000,
            //     icon: 'success'
            //   })
            // }, 500)
            setTimeout(() => {
              wx.showModal({
                title: '提示',
                content: '点击对应会员卡即可进入下单界面',
                showCancel: false,
                confirmText: '我知道了'
              })
            }, 500)
            this.setData({
              logTip: false
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '获取会员卡功能需要登录，是否前往登录',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/tomeslog/tomeslog',
                })
              }
            }
          })
        }
      }
    })

  },
  getAllVipCards: async function () {
    const res = await getMyVip()
    const list=[]
          
       
    // console.log(res);
    if (res.data) {
      console.log(res.data)
      if (res.data.length) {
        list.length=res.data.length
        for (let i = 0; i < res.data.length; i++) {
          res.data[i].status_copy = res.data[i].status
          if (res.data[i].status_copy === -1) res.data[i].status_copy = true
          else res.data[i].status_copy = false
          if(res.data[i].deliveryTime!=null){
            res.data[i].deliveryTime = res.data[i].deliveryTime.substring(0, 10)
          }
          list[i]=false
          if(res.data[i].type=='尊贵VIP会员卡'||res.data[i].type=='至尊VIP会员卡'||res.data[i].type=='荤素礼品年卡B'||res.data[i].type=='蔬菜礼品年卡A'){
            list[i]=true
          }
          res.data[i]['isFlag']=list[i]
          // console.log(res.data)
          if (res.data[i].status === 2 )  res.data[i].status = '已生效'
          else if (res.data[i].status === -1) res.data[i].status = '已冻结'
          else res.data[i].status = '已失效'
          if (res.data[i].type === '蔬菜礼品月卡A') res.data[i].type = 1
          if (res.data[i].type === '荤素礼品月卡B') res.data[i].type = 2
          if (res.data[i].type === '蔬菜礼品年卡A') res.data[i].type = 3
          if (res.data[i].type === '荤素礼品年卡B') res.data[i].type = 4
          if (res.data[i].type === '尊贵VIP会员卡') res.data[i].type = 5
          if (res.data[i].type === '至尊VIP会员卡') res.data[i].type = 6
          if (res.data[i].type === '两无化蔬菜单次卡A') res.data[i].type = 7
          if (res.data[i].type === '两无化蔬菜单次卡B') res.data[i].type = 8
          if (res.data[i].type === '20斤大米提货卡') res.data[i].type = 9
          if (res.data[i].type === '10斤大米礼盒装兑换卡') res.data[i].type = 10
          if (res.data[i].type === '电信劳模尊享套餐卡') res.data[i].type = 11
        }
       
        this.setData({
          vipList: res.data,
          thisShow: true
        })
      } else {
        this.setData({
          thisShow: false
        })
      }
    } else {
      console.log('myvip null');
    }

  },
  // 暂停配送
  stopWeek: function (e) {
    const _this = this
    wx.showModal({
      title: '提示',
      content: '当前操作将会将此卡配送暂停一周，同时已下单未装配的订单将被取消，此操作不可逆，是否确认暂停一周',
      success: async function (res) {
        
        if (res.confirm) {
          const res = await stopGo({
            cardAccount: e.currentTarget.dataset.card
          })
          console.log(res)
          if(res.code!=200){
            console.log("ccc")
            wx.showModal({
              title: '提示',
              content: '暂停失败，请同客服联系',
            })
          }
          console.log(e.currentTarget.dataset.card);
          await _this.getAllVipCards()
        } else console.log('xxx');
      }
    })
  },
  // 下单
  goBuy: function (e) {
    const id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    const card = e.currentTarget.dataset.card
    if (type === 1) type = '蔬菜礼品月卡A'
    if (type === 2) type = '荤素礼品月卡B'
    if (type === 3) type = '蔬菜礼品年卡A'
    if (type === 4) type = '荤素礼品年卡B'
    if (type === 5) type = '尊贵VIP会员卡'
    if (type === 6) type = '至尊VIP会员卡'
    if (type === 7) type = '两无化蔬菜单次卡A'
    if (type === 8) type = '两无化蔬菜单次卡B'
    if (type === 9) type = '20斤大米提货卡'
    if (type === 10) type = '10斤大米礼盒装兑换卡'
    if (type === 11) type = '电信劳模尊享套餐卡'
    wx.navigateTo({
      url: `/pages/buy/buy?id=${id}&type=${type}&card=${card}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    if (!!!wx.getStorageSync('token')) wx.setStorageSync('token', 'token null')
    getUserLogin().then(async res => {
      console.log('res', res);
      if (res.code === 200) {
        this.setData({
          isLog: res.data
        })
        if (!!this.data.token && !!this.data.isLog) {
          this.setData({
            logTip: false
          })
          // setTimeout(() => {
          //   wx.showToast({
          //     title: '登录成功',
          //     duration: 1000,
          //     icon: 'success'
          //   })
          // }, 500)
          setTimeout(() => {
            wx.showModal({
              title: '提示',
              content: '点击对应会员卡即可进入下单界面',
              showCancel: false,
              confirmText: '我知道了'
            })
          }, 500)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setStorageSync('vegetableList', [])
    wx.setStorageSync('meetList', [])
    wx.setStorageSync('eggList', [])
    wx.setStorageSync('riceList', [])
    wx.setStorageSync('fishList', [])
    wx.setStorageSync('bestList', [])
    wx.setStorageSync('cornList', [])
    wx.setStorageSync('fruitList', [])
    wx.setStorageSync('birdList', [])
    wx.setStorageSync('meetliveList', [])
    this.setData({
      vipList: [],
      thisShow: false
    })
    this.setData({
      token: wx.getStorageSync('token'),
    })
    this.getLog()
  },
  goActive() {
    if(!!this.data.token && this.data.isLog) {
      wx.navigateTo({
        url: '/pages/active/active',
      })
    }else {
      wx.showModal({
        title: '提示',
        content: '激活功能需要登录，是否前往登录',
        success(res) {
          if(res.confirm) {
            wx.navigateTo({
              url: '/pages/tomeslog/tomeslog',
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})